package com.da.javatest.algorithm;

/**
 * @author chenlida
 * @date 2019/4/4 15:59
 * @description
 */
public class Tree1 {
    public static void main(String[] args) {
        Tree1 t = new Tree1();
        TreeNode root = t.buildTree();
        t.search1(root);
        System.out.println();
        t.search2(root);
        System.out.println();
        t.search3(root);
    }

    public TreeNode buildTree() {
        TreeNode root = new TreeNode(4);
        TreeNode t1 = new TreeNode(2);
        TreeNode t2 = new TreeNode(6);
        root.setLeft(t1);
        root.setRight(t2);

        TreeNode t3 = new TreeNode(1);
        TreeNode t4 = new TreeNode(3);
        t1.setLeft(t3);
        t1.setRight(t4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(7);
        t2.setLeft(t5);
        t2.setRight(t6);
        return root;
    }

    //二叉树前序遍历
    private void search1(TreeNode root) {
        if (root != null) {
            System.out.print(root.getVal() + " ");
            search1(root.getLeft());
            search1(root.getRight());
        }
    }

    //二叉树中序遍历
    //二叉搜索树的中序遍历就是树节点值的递增排列！
    private void search2(TreeNode root) {
        if (root != null) {
            search2(root.getLeft());
            System.out.print(root.getVal() + " ");
            search2(root.getRight());
        }
    }

    //二叉树后序遍历
    private void search3(TreeNode root) {
        if (root != null) {
            search3(root.getLeft());
            search3(root.getRight());
            System.out.print(root.getVal() + " ");
        }
    }
}
